// 路由守卫
import router from '@/router'

import store from '@/store'

import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

// 白名单
const whiteList = ['/login', '/404']

/**
 * 前置守卫
 *
 * next shiyige
 */
router.beforeEach(async(to, from, next) => {
  nprogress.start()
  // 登录成功
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      // 判断有没有获取到用户信息
      if (!store.getters.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    //   没登录

    // 判断是不是在白名单
    if (whiteList.indexOf(to.path) > -1) {
      next()
    } else {
      next('/login')
    }
  }
  nprogress.done()
})
// 后置守卫
router.afterEach((to, from, next) => {
  nprogress.done()
})
